package com.xy.tool.minio.domain.minio.obj;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xy.tool.minio.domain.constant.BucketConstant;
import com.xy.tool.minio.util.FileUtility;
import com.xy.tool.minio.util.MinioUtil;
import com.xy.tool.minio.util.ServiceConverter;
import com.xy.tool.minio.util.UrlUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


public class FileModel {


    public String type = "desktop";
    public String mode = "edit";
    public String documentType;
    public Document document;
    public EditorConfig editorConfig;
    public String token;

    public FileModel(String fileName, String minObjectName, String uid, String uname, String actionData, MinioUtil minioUtil, FileUtility fileUtility, HttpServletRequest request, String editedDocs, String mode, Boolean down, Boolean pri) throws Exception {
        documentType = fileUtility.GetFileType(minObjectName).toString().toLowerCase();

        document = new Document();
        document.title = fileName;
        document.url = minioUtil.getObjectURL(BucketConstant.DEMOBUCKET, minObjectName, 7);

        document.fileType = fileUtility.getFileExtension(minObjectName).replace(".", "");
        //todo zuihou gengxin riqi
//        document.key = ServiceConverter.GenerateRevisionId(DocumentManager.CurUserHostAddress(null) + "/" + fileName + "/" + Long.toString(new File(DocumentManager.StoragePath(fileName, null)).lastModified()));
        document.key = ServiceConverter.GenerateRevisionId(minObjectName + "/" + minioUtil.lastModified(BucketConstant.DEMOBUCKET, minObjectName) + "000");

        ShareSet shareSet1 = new ShareSet();
        shareSet1.setPermissions("Full Access");
        shareSet1.setUser("John Smith");
        ShareSet shareSet2 = new ShareSet();
        shareSet2.setPermissions("Read Only");
        shareSet2.setUser("Mark Pottato");
        ShareSet shareSet3 = new ShareSet();
        shareSet3.setPermissions("Read Only");
        shareSet3.setUser("Hamish Mitchell");
        ShareSet shareSet4 = new ShareSet();
        shareSet4.setPermissions("Read Only");
        shareSet4.setUser("http://www.baidu.com");
        shareSet4.setLink(true);
        ArrayList<ShareSet> sets = new ArrayList<>();
        sets.add(shareSet1);
        sets.add(shareSet2);
        sets.add(shareSet3);
        sets.add(shareSet4);
        Info info = new Info();
        info.setSharingSettings(sets);
        document.info = info;

        editorConfig = new EditorConfig(actionData);
        editorConfig.callbackUrl = UrlUtil.GetCallback(minObjectName, request);
//        if (lang != null) editorConfig.lang = lang;

        if (uid != null) editorConfig.user.id = uid;
        if (uname != null) editorConfig.user.name = uname;

        editorConfig.customization.goback.url = UrlUtil.GetServerUrl(request) + "/only/index";
        changeType(mode, type, editedDocs, down, pri);
    }

    public void changeType(String _mode, String _type, String editedDocs, Boolean down, Boolean pri) {
        if (_mode != null) mode = _mode;
        if (_type != null) type = _type;

        Boolean canEdit = Arrays.asList(editedDocs.split("\\|")).contains("." + document.fileType.toLowerCase());

        editorConfig.mode = canEdit && !mode.equals("view") ? "edit" : "view";

        document.permissions = new Permissions(mode, type, canEdit, down, pri);

        if (type.equals("embedded")) InitDesktop();
    }

    public void InitDesktop() {
        editorConfig.InitDesktop(document.url);
    }

    /**
     * 没有写生成token的逻辑
     */
    public void BuildToken() {
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("documentType", documentType);
        map.put("document", document);
        map.put("editorConfig", editorConfig);
    }


    public class Document {
        public String title;
        public String url;
        public String fileType;
        public String key;
        public Permissions permissions;
        public Info info;
    }


    public class Info {
        public List<ShareSet> sharingSettings;

        public List<ShareSet> getSharingSettings() {
            return sharingSettings;
        }

        public void setSharingSettings(List<ShareSet> sharingSettings) {
            this.sharingSettings = sharingSettings;
        }
    }

    public class ShareSet {
        public String getPermissions() {
            return permissions;
        }

        public void setPermissions(String permissions) {
            this.permissions = permissions;
        }

        public String getUser() {
            return user;
        }

        public void setUser(String user) {
            this.user = user;
        }

        public boolean isLink() {
            return isLink;
        }

        public void setLink(boolean link) {
            isLink = link;
        }

        private String permissions;
        private String user;
        private boolean isLink;
    }

    public class Permissions {
        public Boolean comment;
        public Boolean download;
        public Boolean edit;
        public Boolean fillForms;
        public Boolean modifyFilter;
        public Boolean modifyContentControl;
        public Boolean review;
        public Boolean print;

        /**
         * onlyOffice权限定义
         *
         * @param mode    读写
         * @param type
         * @param canEdit
         * @param down
         */
        public Permissions(String mode, String type, Boolean canEdit, Boolean down, Boolean pri) {
            comment = !mode.equals("view") && !mode.equals("fillForms") && !mode.equals("embedded") && !mode.equals("blockcontent");
            download = down;
            print = pri;
            edit = canEdit && (mode.equals("edit") || mode.equals("filter") || mode.equals("blockcontent"));
            fillForms = !mode.equals("view") && !mode.equals("comment") && !mode.equals("embedded") && !mode.equals("blockcontent");
            modifyFilter = !mode.equals("filter");
            modifyContentControl = !mode.equals("blockcontent");
            review = mode.equals("edit") || mode.equals("review");
        }
    }

    public class EditorConfig {
        public HashMap<String, Object> actionLink = null;
        public String mode = "edit";
        public String callbackUrl;
        //TODO 改成zh
        public String lang = "zh";//en
        public User user;
        public Customization customization;
        public Embedded embedded;

        public EditorConfig(String actionData) {
            if (actionData != null) {
                Gson gson = new Gson();
                actionLink = gson.fromJson(actionData, new TypeToken<HashMap<String, Object>>() {
                }.getType());
            }
            user = new User();
            customization = new Customization();
        }

        public void InitDesktop(String url) {
            embedded = new Embedded();
            embedded.saveUrl = url;
            embedded.embedUrl = url;
            embedded.shareUrl = url;
            embedded.toolbarDocked = "top";
        }

        public class User {
            public String id = "uid-1";
            public String name = "John Smith";
        }

        public class Customization {
            public Goback goback;
            public Logo logo;

            public Customization() {
                goback = new Goback();
                logo = new Logo();
            }

            public class Goback {
                public String url;
            }

            public class Logo {
                public String image;
                public String url;
            }
        }

        public class Embedded {
            public String saveUrl;
            public String embedUrl;
            public String shareUrl;
            public String toolbarDocked;
        }
    }


    public static String Serialize(FileModel model) {
        Gson gson = new Gson();
        return gson.toJson(model);
    }
}
